<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  <head>
    <title>Building for Android</title>
    <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
    <link rel="stylesheet" type="text/css" href="kawa.css"/>
    <script src="kawa-ebook.js" type="text/javascript"/>
    <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
    <link rel="prev" href="Overall-Index.xhtml" title="Index"/>
    <link rel="next" href="Android-view-construction.xhtml" title="Android view construction"/>
  </head>
  <body>
    <header/>
    <section class="sect1" title="Building for Android" epub:type="subchapter" id="Building-for-Android">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both">Building for Android</h2>
          </div>
        </div>
      </div>
      <p>Google’s phone/tablet operating system
<a class="ulink" href="https://developers.google.com/android/" target="_top">Android</a>
is based on a custom virtual machine on top of a Linux kernel.
Even though Android isn’t strictly (or legally) speaking Java,
you can build Android applications using Kawa.
</p>
      <p>Below is "Hello world" written in Kawa Scheme.  A slightly
more interesting example is in <a class="link" href="Android-view-construction.xhtml" title="Android view construction">next section</a>.
</p>
      <pre class="screen">(require 'android-defs)
(activity hello
  (on-create-view
   (android.widget.TextView (this)
    text: "Hello, Android from Kawa Scheme!")))
</pre>
      <p>The following instructions have been tested on GNU/Linux,
specifically Fedora 17.
<a class="ulink" href="http://asieno.com/blog/index.php/post/2012/08/16/Setting-up-the-environment-Android-Kawa" target="_top">This link</a> may be helpful if you’re building on Windows.
</p>
      <section class="sect2" title="Downloading and setting up the Android SDK" epub:type="division" id="idm139667869672672">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title">Downloading and setting up the Android SDK</h3>
            </div>
          </div>
        </div>
        <p>First <a class="ulink" href="http://code.google.com/android/download.html" target="_top">download the Android SDK</a>. Unzip in a suitable location, which we’ll refer to as <code class="literal">ANDROID_HOME</code>.
</p>
        <pre class="screen">export ANDROID_HOME=/path/to/android-sdk-linux
PATH=$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH
</pre>
        <p>Next you have to get the appropriate platform SDK:
</p>
        <pre class="screen">$ android update sdk
</pre>
        <p>You need to select an Android “platform”.
Platform (API) 16 corresponds to Android 4.1.2 (Jelly Bean).
Select that or whatever you prefer, and click <code class="literal">Install</code>.
(You can install multiple platforms, but each project
is built for a specific platform.)
</p>
        <pre class="screen">ANDROID_PLATFORM=android-16
</pre>
      </section>
      <section class="sect2" title="Building Kawa for Android" epub:type="division" id="idm139667869666944">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title">Building Kawa for Android</h3>
            </div>
          </div>
        </div>
        <p>Set <code class="literal">JAVA_HOME</code> to where your JDK tree is.
You should use JDK 6; JDK 7 does not work at time of writing.
</p>
        <pre class="screen">$ export JAVA_HOME=/opt/jdk1.6
</pre>
        <p>First <a class="link" href="Getting-Kawa.xhtml" title="Getting Kawa">get the Kawa source code</a>.
</p>
        <p>If using Ant (as is recommended on Windows):
</p>
        <pre class="screen">$ ant -Denable-android=true
</pre>
        <p>Alternatively, you can use <code class="literal">configure</code> and <code class="literal">make</code>:
</p>
        <pre class="screen">$ KAWA_DIR=path_to_Kawa_sources
$ cd $KAWA_DIR
$ ./configure --with-android=$ANDROID_HOME/platforms/$ANDROID_PLATFORM/android.jar --disable-xquery --disable-jemacs
$ make
</pre>
      </section>
      <section class="sect2" title="Creating the application" epub:type="division" id="idm139667869660736">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title">Creating the application</h3>
            </div>
          </div>
        </div>
        <p>Next, we need to create a project or “activity”.
This tutorial assumes you want to create the project
in the target directory <code class="literal">KawaHello</code>,
with the main activity being a class named <code class="literal">hello</code> in a
package <code class="literal">kawa.android</code>:
</p>
        <pre class="screen">PROJECT_DIR=KawaHello
PROJECT_CLASS=hello
PROJECT_PACKAGE=kawa.android
PROJECT_PACKAGE_PATH=kawa/android
</pre>
        <p>To create the project use the following command:
</p>
        <pre class="screen">$ android create project --target $ANDROID_PLATFORM --name $PROJECT_DIR --activity $PROJECT_CLASS --path ./$PROJECT_DIR --package $PROJECT_PACKAGE
</pre>
        <p>Replace the skeleton <code class="literal">hello.java</code> by the Scheme code at the
top of this note, placing in a file named <code class="literal">hello.scm</code>:
</p>
        <pre class="screen">$ cd $PROJECT_DIR
$ HELLO_APP_DIR=`pwd`
$ cd $HELLO_APP_DIR/src/$PROJECT_PACKAGE_PATH
$ rm $PROJECT_CLASS.java
$ <span class="emphasis"><em>create</em></span> $PROJECT_CLASS.scm
</pre>
        <p>We need to copy/link the Kawa jar file so the Android SDK can find it:
</p>
        <pre class="screen">$ cd $HELLO_APP_DIR
$ ln -s $KAWA_DIR/kawa-2.92_invoke.jar libs/kawa.jar
</pre>
        <p>Optionally, you can use kawart-2.92_invoke.jar, which is slightly smaller,
but does not support eval, and does not get built by the Ant build:
</p>
        <pre class="screen">$ ln -s $KAWA_DIR/kawart-2.92_invoke.jar libs/kawa.jar
</pre>
        <p>Copy or link <code class="literal">custom_rules.xml</code> from the Kawa sources:
</p>
        <pre class="screen">ln -s $KAWA_DIR/gnu/kawa/android/custom_rules.xml .
</pre>
        <p>Finally to build the application just do:
</p>
        <pre class="screen">$ ant debug
</pre>
      </section>
      <section class="sect2" title="Running the application on the Android emulator" epub:type="division" id="idm139667869650560">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title">Running the application on the Android emulator</h3>
            </div>
          </div>
        </div>
        <p>First you need to create an <a class="ulink" href="http://developer.android.com/tools/devices" target="_top">Android Virtual Device (avd)</a>. Start:
</p>
        <pre class="screen">android
</pre>
        <p>Then from menu <code class="literal">Tools</code> select <code class="literal">Manage AVDs...</code>.
In the new window click <code class="literal">New....</code>
Pick a <code class="literal">Name</code> (we use <code class="literal">avd16</code> in the following),
a <code class="literal">Target</code> (to match <code class="literal">$ANDROID_PLATFORM</code>),
and optionally change the other properties, before clicking <code class="literal">Create AVD</code>.
</p>
        <p>Now you can start up the Android emulator:
</p>
        <pre class="screen">$ emulator -avd avd16 &amp;
</pre>
        <p>Wait until Android has finished booting (you will see the Android home screen),
click the menu and home buttons. Now install our new application:
</p>
        <pre class="screen">adb install bin/KawaHello-debug.apk
</pre>
      </section>
      <section class="sect2" title="Running the application on your device" epub:type="division" id="idm139667869642320">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title">Running the application on your device</h3>
            </div>
          </div>
        </div>
        <p>If the emulator is running, kill it:
</p>
        <pre class="screen">$ kill %emulator
</pre>
        <p>On your phone or other Android devude, enable USB debugging.
(This is settable from the <code class="literal">Settings</code> application,
under <code class="literal">Applications / Development</code>.)
</p>
        <p>Connect the phone to your computer with the USB cable.
Verify that the phone is accessible to <code class="literal">adb</code>:
</p>
        <pre class="screen">$ adb devices
List of devices attached 
0A3A560F0C015024	device
</pre>
        <p>If you don’t see a device listed, it may be permission problem. You can figure out which device corresponds to the phone by doing:
</p>
        <pre class="screen">$ ls -l /dev/bus/usb/*
/dev/bus/usb/001:
total 0
...
crw-rw-rw- 1 root wheel 189, 5 2010-10-18 16:52 006
...
</pre>
        <p>The timestamp corresponds to when you connected the phone.
Make the USB connection readable:
</p>
        <pre class="screen">$ sudo chmod a+w /dev/bus/usb/001/006
</pre>
        <p>Obviously if you spend time developing for an Androd phone you’ll want to automate this process;
<a class="ulink" href="https://sites.google.com/site/siteofhx/Home/android/drivers/udc" target="_top">this link</a>
or <a class="ulink" href="https://groups.google.com/forum/?fromgroups=#!topic/android-developers/nTfhhPktGfM" target="_top">this link</a> may be helpful.
</p>
        <p>Anyway, once <code class="literal">adb</code> can talk to the phone, you install in the same way as before:
</p>
        <pre class="screen">adb install bin/KawaHello-debug.apk
</pre>
      </section>
      <section class="sect2" title="Some debugging notes" epub:type="division" id="idm139667869632608">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title">Some debugging notes</h3>
            </div>
          </div>
        </div>
        <p>You will find a copy of the SDK documentation in <code class="literal">$ANDROID_HOME/docs/index.html</code>.
</p>
        <p>If the emulator complains that your application has stopped unexpectedly, do:
</p>
        <pre class="screen">$ adb logcat
</pre>
        <p>This shows log messages, stack traces, output from the <code class="literal">Log.i</code> logging method, and other useful information.
(You can alternatively start <code class="literal">ddms</code> (Dalvik Debug Monitor Service), click on the <code class="literal">kawa.android line</code> in the top-left sub-window to select it, then from the <code class="literal">Device</code> menu select <code class="literal">Run logcat....</code>).
</p>
        <p>To uninstall your application, do:
</p>
        <pre class="screen">$ adb uninstall kawa.android
</pre>
      </section>
      <section class="sect2" title="Other resources" epub:type="division" id="idm139667869626352">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title">Other resources</h3>
            </div>
          </div>
        </div>
        <p>(A more interesting <a class="ulink" href="http://androidscheme.blogspot.com/2010/10/text-to-speech-app.html" target="_top">text-to-speech</a> example app is on Santosh Rajan’s <a class="ulink" href="http://androidscheme.blogspot.com/" target="_top">Android-Scheme blog</a>.)
</p>
        <p><a class="ulink" href="https://github.com/ecraven/SchemeAndroidOGL" target="_top">https://github.com/ecraven/SchemeAndroidOGL</a>
</p>
      </section>
    </section>
    <footer>
      <div class="navfooter">
        <ul>
          <li>
            <b class="toc">
              <a href="Building-for-Android.xhtml#idm139667869672672">Downloading and setting up the Android SDK</a>
            </b>
          </li>
          <li>
            <b class="toc">
              <a href="Building-for-Android.xhtml#idm139667869666944">Building Kawa for Android</a>
            </b>
          </li>
          <li>
            <b class="toc">
              <a href="Building-for-Android.xhtml#idm139667869660736">Creating the application</a>
            </b>
          </li>
          <li>
            <b class="toc">
              <a href="Building-for-Android.xhtml#idm139667869650560">Running the application on the Android emulator</a>
            </b>
          </li>
          <li>
            <b class="toc">
              <a href="Building-for-Android.xhtml#idm139667869642320">Running the application on your device</a>
            </b>
          </li>
          <li>
            <b class="toc">
              <a href="Building-for-Android.xhtml#idm139667869632608">Some debugging notes</a>
            </b>
          </li>
          <li>
            <b class="toc">
              <a href="Building-for-Android.xhtml#idm139667869626352">Other resources</a>
            </b>
          </li>
        </ul>
        <p>
          Up: <a accesskey="u" href="Miscellaneous.xhtml">Miscellaneous topics</a></p>
        <p>
        Previous: <a accesskey="p" href="Building-JavaFX-applications.xhtml">Building JavaFX applications</a></p>
        <p>
        Next: <a accesskey="n" href="Android-view-construction.xhtml">Android view construction</a></p>
      </div>
    </footer>
  </body>
</html>
